#! /usr/bin/python
#                   _ooOoo_                     
#                  o8888888o                    
#                  88" . "88                    
#                  (| -_- |)                    
#                  O\  =  /O                    
#               ____/`---'\____                 
#             .'  \\|     |//  `.               
#            /  \\|||  :  |||//  \              
#           /  _||||| -:- |||||-  \             
#           |   | \\\  -  /// |   |             
#           | \_|  \---/  |   |             
#           \  .-\__  `-`  ___/-. /             
#         ___`. .'  /--.--\  `. . __            
#      ."" '<  `.___\_<|>_/___.'  >'"".      
#     | | :  `- \`.;`\ _ /`;.`/ - ` : | |       
#     \  \ `-.   \_ __\ /__ _/   .-` /  /       
#======`-.____`-.___\_____/___.-`____.-'======  
#                   `=---='                     
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
#         Buddha bless me for no bug            
#                                               
####################################################################################
#   Filename    :  runcmd.py                                                      
#   Author      :  Shawn Tan                                                        
#   Date        :  2017.2.22                                                        
#   Description :                                                                   
#   History     :  2017.2.22             creat the document            Shawn Tan    
####################################################################################

from optparse import OptionParser 
from StarVar import * 
from StarInfo import *

__author__ = 'ST-CAT'

def cmd_getcmd():

  ########################################################################################
  #                                 Solve Command Line 
  ########################################################################################
  parser = OptionParser()
  # Flow Instruction
  parser.add_option("--guide", action="store_true", default=False, \
                    help="Show the tools user guide/terminology")
  # File/Path
  parser.add_option("-l", action="store", default=None, dest="Tdeflist", \
                    help="Import a parser.rc file (a list which records many tdefs )")
  parser.add_option("--tdef", action="store", default=None, dest="TdefFiles", \
                    help="Import a single tdef file\n")
  parser.add_option("-d", action="store", default=None, dest="ReDir", 
                    help="Appoint the path you want to start simulation")
  #Flow Control
  parser.add_option("--drm", action="store", dest='DRM', default=None, 
                    help="Specify simulation working mode: 1. lsf 2. parallel_local 3. serial_local")
  parser.add_option("-p", "--ParallelNum", action="store", default=None, dest="ParallelNum", 
                    help='Specify simulation parallel num in "parallel_local" mode')
  parser.add_option("-n", action="store", dest='RunNum', type=int, default=1, 
                    help="Specify execution number of every testcase in one run time")
  parser.add_option("--log", action="store_true", dest='LOG_ON', default=False, 
                    help="Use Xterm window emulator to display simlation log ")
  parser.add_option("--nq", action="store_true", dest='NoQuiet', default=False, 
                    help="Disable Log Quiet mode, defualt is quiet")
  parser.add_option("--lsf", action="store_true", dest='COMPILE_LSF',  default=False, 
                    help="Submit the compile to OpenLava")
  parser.add_option("--debug", "--Debug", dest='Debug', action="store_true", default=False, 
                    help="Enable case debug feature of runsim, this feature will demonstrate every test configuration without execution")
  
  parser.add_option("--step", dest='Step', action="store_true", default=False, 
                    help="Enable step by step debug feature for runsim, this feature will demonstrate every test configuration without operation")
  
  parser.add_option("-v", action="store_true", dest='VIM_VIEW',  default=False, 
                    help="Open simulation log by vim")
  parser.add_option("-g", action="store_true", dest='GVIM_VIEW', default=False, 
                    help="Open simulation log by gvim")
  parser.add_option("-e", action="store_true", dest='EMACS_VIEW',default=False, 
                    help="Open simulation log by emacs")
  
  parser.add_option("--list", action="store_true", dest='LIST',  default=False, 
                    help="List all testcase in tdef")
  
  parser.add_option("--clean", action="store_true", dest='CLEAN',  default=False, 
                    help="Clean the compile garbage by remove the entire compile dir, it's a standalone command")
  parser.add_option("-c", action="store_true", dest='CLEAN_WITH_OTHER',  default=False, 
                    help="Clean the compile garbage by remove the entire compile dir before compile, it must do with a compile ")
  parser.add_option("--rescan", action="store_true", dest='RESCAN',  default=False, 
                    help="Check the testcase result again without doing the simualtion again")
  parser.add_option("--check_log", action="store_true", dest='CHECK_LOG',  default=False, 
                    help='''Check simulation log fail details''') 

  parser.add_option("--vendor", action="store", dest='VENDOR',  default='synopsys', 
                    help='''define the EDA tools vendor ''') 

  parser.add_option("--gls", action="store", dest='LSF_OPT',  default=None, 
                    help=''' enable gate level simulation option  ''') 

  parser.add_option("--lsf_opt", action="store_true", dest='GLS',  default=None, 
                    help=''' add openlava command while submmitting job  ''') 
  
  # Task Option
  parser.add_option("-t", "--TestName", action="store", default=None, dest="Test",\
                    help='''
                    the TEST/GROUP/SESSION which will join regression,  
                    the format will be :
                    1. SESSION/GROUP/TEST 
                    2. SESSION/GROUP
                    3. GROUP
                    4. SESSION
                    5. TEST
                    ''')
  parser.add_option("--seed", action="store",type=int, default=None, dest="SEED", 
                    help='''Define a seed for simulation
                    ''')
  parser.add_option("--cov",  action="store_true",dest='COVERAGE', default=False, 
                    help="Enable Coverage Collecting")
  parser.add_option("--wave", action="store_true", dest='DUMPWAVE', default=False, 
                    help='''Enalbe Dumping Waveform
                    ''')
  parser.add_option("--comp", action="store_true", dest='CompileEnable', default=False, 
                    help="Add all actions about compile(include RTL/VIP/TB compile&elaboration)")
  parser.add_option("--comp_rtl", action="store_true", dest='CompileRTLEnable', default=False, 
                    help="Add RTL Compile actions")
  parser.add_option("--comp_vip", action="store_true", dest='CompileVIPEnable', default=False, 
                    help="Add VIP Compile actions")
  parser.add_option("--comp_tb", action="store_true", dest='CompileTBEnable', default=False, 
                    help="Add TestBench Compile actions")
  parser.add_option("--elab", action="store_true", dest='ElaborationEnable', default=False, 
                    help="Add elaboration actions")
  parser.add_option("--sim", action="store_true", dest='SimulationEnable',default=False, 
                    help="Add simulation Actions")
  parser.add_option("--comp_dir", dest='Compile_Dir', action="store", default=None,  
                    help="Name the compile dir in a compile actions to replace default")
  parser.add_option("--sim_dir", dest='Sim_Dir', action="store", default=None,  
                    help="Name the compile dir in a simulation actions to replace default")
  parser.add_option("--comp_rtl_log", dest= 'COMP_RTL_LOG', action="store", default='rtl_sum.log',  
                    help="Name rtl compile log to replace default")
  parser.add_option("--comp_tb_log", dest= 'COMP_TB_LOG', action="store", default='tb_sum.log',  
                    help="Name Testbench compile log to replace default")
  parser.add_option("--comp_vip_log", dest= 'COMP_VIP_LOG', action="store", default='vip_sum.log',  
                    help="Name VIP compile log to replace default ")
  parser.add_option("--elab_log", dest= 'ELAB_LOG', action="store", default='elab.log',  
                    help="Name the elab log name to replace default")
  parser.add_option("--sim_log", dest= 'SIM_LOG', action="store", default='sim.log',  
                    help="Name sim log to replace default")
  parser.add_option("--verdi", action="store_true", dest='VERDI',  default=False, 
                    help="open Verdi")
  parser.add_option("--comp_opt", action="store", dest='COMP_OPT',  default=None, 
                    help="add user-defined compile option in compile actions ")
  parser.add_option("--comp_rtl_opt", action="store", dest='COMP_RTL_OPT',  default=None, 
                    help="add user-defined compile rtl option in compile actions ")
  parser.add_option("--comp_vip_opt", action="store", dest='COMP_VIP_OPT',  default=None, 
                    help="add user-defined compile vip option in compile actions ")
  parser.add_option("--comp_tb_opt", action="store", dest='COMP_TB_OPT',  default=None, 
                    help="add user-defined compile testbench option in compile actions ")
  parser.add_option("--elab_opt", action="store", dest='ELAB_OPT',  default=None, 
                    help="add user-defined elaboration option in compile actions")
  parser.add_option("--verdi_opt", action="store", dest='VERDI_OPT',  default=None, 
                    help="add user-defined elaboration option in compile actions")
  #parser.add_option("--upf_path", action="store", dest='UPF_PATH', default=None, 
  #                  help="give the upf path")
  #parser.add_option("--sdf_config_files", action="store", dest='SDF_CFG_FILE', 
  #                   default=None, help="give the sdf path")
  #parser.add_option("--git_cfg", action="store", dest='GIT_CFG', type=int, default=None, 
  #                  help="define star_git cfg file path")
  parser.add_option("--ucli", action="store_true", dest='UCLI',  default=False, 
                    help="Enable ucli mode")
  parser.add_option("--profile_time", action="store_true", dest='PROFILE_TIME',  default=False, 
                    help="Performance analysis command, Enable simulation time cost profiling")
  parser.add_option("--profile_mem", action="store_true", dest='PROFILE_MEM',  default=False, 
                    help="Performance analysis command, Enable simulation server memory cost profiling")
  parser.add_option("-r", action="store_true", dest='RERUN',  default=False, 
                    help="Find the failed case and start rerun rerun")
  parser.add_option("--c_test", action="store_true", dest='C_TEST',  default=False, 
                    help="Do SoC C test only in simulation for debug")
  
  # Tdef Param
  parser.add_option("--Verbosity", action="store", default=None, 
                    help="Set UVM verbosity level")
  parser.add_option("--vip", action="store", dest='VIPFILE',default = None, 
                    help="Give vip filelist name")
  parser.add_option("--tb", action="store", dest='TBFILE',  default = None, 
                    help="Give tb filelist name")
  parser.add_option("--rtl", action="store", dest='RTLFILE',default = None, 
                    help="Give rtl filelist name")
  parser.add_option("--cf", action="store", dest='CFILE',   default = None, 
                    help="Give c filelist name")
  parser.add_option("--pre_session_script", action="store",  dest='PRE_SESSION_SCRIPT', default=None, 
                    help="Give pre_session_script name")
  parser.add_option("--session_script", action="store", dest='SESSION_SCRIPT', default=None, 
                    help="Give session_script name")
  parser.add_option("--scan_script", action="store",dest='SCAN_SCRIPT', default=None, 
                    help="Give scan_script name")
  parser.add_option("--run_script", action="store",dest='RUN_SCRIPT', default=None, 
                    help="Give run_script name")
  parser.add_option("--pre_run_script", action="store",dest='PRE_RUN_SCRIPT', default=None, 
                    help="Give pre_run_script name")
  parser.add_option("--post_run_script", action="store",dest='POST_RUN_SCRIPT', default=None, 
                    help="Give post_run_script name ")
  parser.add_option("--post_simulation_script", action="store", dest='POST_SIMULATION_SCRIPT', default=None, 
                    help="Give post_simulation_script name")
  parser.add_option("--post_session_script", action="store", dest='POST_SESSION_SCRIPT', default=None, 
                    help="Give post_session_script name")
  parser.add_option("--pre_group_script", action="store",dest='PRE_GROUP_SCRIPT', default=None, 
                    help="Give pre_group_script name")
  parser.add_option("--post_group_script", action="store",dest='POST_GROUP_SCRIPT', default=None, 
                    help="Give post_group_script name")
  parser.add_option("--pass_cfg", action="store",dest='PASS_CFG', default=None, 
                    help="Give pass_cfg name")
  parser.add_option("--fail_cfg", action="store",dest='FAIL_CFG', default=None, 
                    help="Give fail_cfg name")
  parser.add_option("--ignore_cfg", action="store",dest='IGNORE_CFG', default=None, 
                    help="Give ignore_cfg name")
  parser.add_option("--simargs", action="store", dest='SIMARGS', default=None, 
                    help="input the simargs parameter")
  parser.add_option("--simargs_plus", action="store", dest='SIMARGS_PLUS', default=None, 
                    help="input the simargs parameter")
  parser.add_option("--uvm_testname", action="store", dest='UVM_TESTNAME', default=None, 
                    help="give the uvm_testname")
  parser.add_option("--c_testname", action="store", dest='C_TESTNAME', default=None, 
                    help="give the c_testname")
  parser.add_option("--cat", action="store", dest='CATEGORY', default=None, 
                    help="give a category of a test set ")
  
  (option, args) = parser.parse_args()

  OpPipe = []
  simoplist=[]

  #Class 1 command:
  if option.RESCAN:
    assert option.Test != None or option.ReDir, cmd001
    class1_op = 'RUNSIM_RESCAN'
  elif option.LIST: 
    class1_op = 'RUNSIM_LIST'
  elif (option.GVIM_VIEW or option.EMACS_VIEW or option.VIM_VIEW):
    assert option.Test != None and option.SEED != None, cmd002
    class1_op = 'RUNSIM_VIEW'
  elif option.VERDI:
    assert option.Test != None, cmd000
    class1_op = 'RUNSIM_VERDI'
  elif option.CLEAN:
    assert option.Test != None, cmd000
    class1_op = 'RUNSIM_CLEAN'
  elif option.RERUN:
    assert option.Test != None or option.ReDir, cmd001
    class1_op = 'RUNSIM_RERUN'
  else:
    assert option.Test != None, cmd000
    class1_op = 'RUNSIM_ACT'

    if option.Step:
      simoplist.append("RUNSIM_DEBUG")
    elif option.C_TEST:
      simoplist.append("RUNSIM_C_TEST")
    else: 
      if option.CompileEnable:
        simoplist.append("RUNSIM_COM_RTL_VIP_TB_ELAB")
      else: 
        assert option.Test != None, cmd000
        if option.CompileRTLEnable: 
          simoplist.append("RUNSIM_COM_RTL")

        if option.CompileVIPEnable: 
          if len(simoplist) == 0: 
            simoplist.append("RUNSIM_COM_VIP")
          else: 
            simoplist[0] = simoplist[0]+'_VIP'

        if option.CompileTBEnable: 
          if len(simoplist) == 0: 
            simoplist.append("RUNSIM_COM_TB")
          else: 
            simoplist[0] = simoplist[0]+'_TB'

        if option.ElaborationEnable: 
          if len(simoplist) == 0: 
            simoplist.append("RUNSIM_COM_ELAB")
          else: 
            simoplist[0] = simoplist[0]+'_ELAB'

      if option.SimulationEnable:
        if len(simoplist) == 0: 
          simoplist.append("RUNSIM_SIM")
        else:
          simoplist[0] = simoplist[0]+'_SIM'

      if len(simoplist) == 0:
        simoplist=['RUNSIM_COM_RTL_VIP_TB_ELAB_SIM']

  OpPipe.append(class1_op)

  if len(simoplist) !=0:
    OpPipe.extend(simoplist)
 
  return option, OpPipe


